var pro_load_page = 2,pro_load_flag = true;
var searchResult = {
	init : function(){
		var data=window.sessionStorage.getItem('searchResult');
		data = JSON.parse(data);
		var data1={};
		keyw =data.key;
		console.log(data.key);
		if(data.type){
			console.log(data.type);
			data1={
				stock_id : keyw
			}
		}else{
			data1 = {
				keyword : keyw
			}
		}
		console.log(data);
		app.ajax(
			'/Goods/Index/keywords_search',
			data1,function(res){
				if(res.code == 200){
					app.tpl($('.searchR-list'),'tpl-searchR-list',res);
					app.tpl($('.filter-head'),'tpl-filter-head',res);
					echo.init();
				}else{
					layer.msg(res.msg);
				}
			}
		);
	},
	addCart:function(t,pageX,pageY,pL){
		var addcar=$(t),
		cartPx=pageX,
		cartPy=pageY,
		cartTop=$(t).offset().top;
		this.skuId = $(t).attr("data-skuid");
		console.log(cartPy,cartPx);
		this.warehouseType = $(t).attr("data-warehousetype"); //仓库id
		this.supplierId = $(t).attr("data-supplierid");//供应商id
		console.log(this.supplierId);
		this.cartTop = $(t).offset().top;
		docTop=$(document).scrollTop();
		var data = {
			skuid:this.skuId,
			buyPoductNum :1,
			warehouseType : this.warehouseType,
			supplierId : this.supplierId
		}
		app.ajax(
			'/Goods/Cart/add_cart',
			data,function(res){
				console.log(JSON.stringify(res));
				if(res.code == 200){
					$('footer .index-cart i').text(res.num);
					var img = addcar.parent().parent().siblings('.img-p').find('img').attr('src');
					// 抛物线元素的的位置标记
					var flyer = $('<img class="u-flyer" src="'+img+'" width="60px" height="60px" style="z-index:350">');
					flyer.fly({
						start: {
							left: pL+10,
							top: cartTop-docTop+10
						},
						end: {
							left: cartPx,
							top: cartPy,
							width: 0,
							height: 0
						},
						onEnd: function(){
							$("#msg").show().animate({width: '250px'}, 200).fadeOut(1000);
							addcar.css("cursor","default").removeClass('orange').unbind('click');
							this.destory();
						}
					});
				}else if(res.code == 300){
					alert(res.msg);
				}
			}
		);			
	},
	filterSearch : function(key1,data,type,title){
		var title = title;
		var data1={};
		data1[""+key1]=data;
		app.ajax(
			'/Goods/Index/keywords_search',
			data1,function(res){
				if(res.code == 200){
					app.tpl($('.searchR-list'),'tpl-searchR-list',res);
					app.tpl($('.filter-head'),'tpl-filter-head',res);
					if(type == 'classify'){
						$('.filter-head .classify p').text(title);
					}else if(type == 'news'){
						$('.filter-head .filter .new p').text(title);
					}
					echo.init();
				}else{
					layer.msg(res.msg);
				}
			}
		);
	},
	loadMore: function(url,key1,data){
		var listLoadingEmpty = '<div style="height:3rem;line-height:3rem;text-align:center;font-size:1.2rem;" class="no-more">努力加载中~<div>';
   		$('.product-list').append(listLoadingEmpty);
   		var data1 ={};
 		data1[""+key1] =data;
 		data1.page=pro_load_page;
   		
		pro_load_flag = false;
		app.ajax(
			url,
			data1,
			function(res){
				$('.product-list .no-more').remove();
				if(res.code == 200){
					var html='';
					if(res.res.list.length > 0){
						app.tpladd($('.searchR-list'),'tpl-searchR-list',res);
						var list =res.res.list;
						echo.init();
						for(var i=0;i< list.length;i++){
							var shou ='';
							if(list[i].stock <1){
								shou ="<div class='sale-out'><div class='text'>已售馨</div></div>";
							}
							html+= "<li class='fl w50' data-pid='"+list[i].pid+"'><div class='content bGWhite oF pL7 pR7' ><div class='img-p pT10 oF pSR'  data-id='"+list[i].id+"'>"
									+"<img src='../img/blank.gif' data-echo='"+list[i].pic+"' class='w'/>"+shou+"</div><div class='list-detail pL5 '><div class='list-title w'>"
									+"<h2 class='fS14 lH18 limit-text mT14'>"+list[i].pname+"</h2></div><div class='detail w'><p class='fl mT20 mB15'><span class='cRed fS15'>"
									+"&yen;"+list[i].price+"</span><s class='pL5 cGrey fS12'>&yen;"+list[i].market_price+"</s></p><div class='list-btn ddn-toCart mT10 mR3 tC "
									+"bR50 bGBlack fr' data-skuId='"+list[i].id+"' data-warehousetype='"+list[i].warehouseType+"' data-supplierId='"+list[i].supplierId+"' "
									+"data-stock='"+list[i].stock+"'><img src='../img/list/cart1.png' class='w70 mT7'/></div></div></div><div></li>";
						}
						$('.product-list ul').append(html);
						pro_load_page++;
						pro_load_flag = true;
					//page++;
					}else{						
						var listLoadingEmpty = '<div style="height:3rem;line-height:3rem;text-align:center;font-size:1.2rem;">没有更多数据了~<div>';
						$('.product-list').append(listLoadingEmpty);
					}
				}else{
					layer.msg('系统繁忙，请稍候再试');
				}
			}
		);	
	}
}

footer.init();
var data=window.sessionStorage.getItem('searchResult');
data = JSON.parse(data);
var key =data.key;
	if(data.type){
			header.init('搜索');
	}else{
		header.init(key);
	}
searchResult.init();
	
	  var ua = window.navigator.userAgent.toLowerCase(); 
      if(ua.match(/MicroMessenger/i) != 'micromessenger'){ 
      	$('.searchR-list').css('marginTop','7.42rem');
      }
      
window.onload = function(){
	$('body').on('click','.filter .news',function(){
		if($('.filter-bg').hasClass('dN')){
			$('.filter-bg').removeClass('dN');
			if($('header').html()){
				$('.filter-bg').addClass('filter-bgT');
			}
			$('.filter-bg .news').show();
			
		}else{
			$('.filter-bg').addClass('dN');
			$('.filter-bg .news').hide();
		}
	});
	
	//分仓筛选
	$('body').on('click','.filter-bg .news .btn',function(){
		$('.filter-bg .news .btn').removeClass('btn-black');
		$('.filter-bg .news .btn').addClass('btn-white');
		$(this).removeClass('btn-white');
		$(this).addClass('btn-black');
		var desc1 = $(this).attr('data-desc');	
		if(desc1 != 'cang'){
			$('.filter-bg').hide();
			window.sessionStorage.setItem('searchkeyname',desc1);
			window.sessionStorage.setItem('searchdata','desc');
			var pro_load_page = 2,pro_load_flag = true;
			var type='news';
			var title = $(this).text();
			searchResult.filterSearch(desc1,'desc',type,title);
			$('.filter-head .filter .new p').text(title);
		}else{
			if($('.filter-bg .news .cang').hasClass('dN')){
				$('.filter-bg .news .cang').removeClass('dN');
			}else{
				$('.filter-bg .news .cang').addClass('dN');
			}
		}
	});
	
	//仓筛选
	$('body').on('click','.filter-bg .news .cang .btn',function(){
		$('.filter-bg .news .cang .btn').removeClass('btn-black');
		$('.filter-bg .news .cang .btn').addClass('btn-white');
		$(this).removeClass('btn-white');
		$(this).addClass('btn-black');
		$('.filter-bg').hide();
		var pro_load_page = 2,pro_load_flag = true;
		var data = $(this).attr('data-stockId');
		window.sessionStorage.setItem('searchkeyname','stock_id');
		window.sessionStorage.setItem('searchdata',data);
		var title = $(this).text();
		searchResult.filterSearch('stock_id',data);
	});
	
	//全部商品
	$('body').on('click','.filter .all.classify',function(){
		if($('.filter-bg').hasClass('dN')){
			$('.filter-bg').removeClass('dN');
			if($('header').html()){
				$('.filter-bg').addClass('filter-bgT');
			}
			$('.filter-bg .all').show();
			
		}else{
			$('.filter-bg').addClass('dN');
			$('.filter-bg .all').hide();
		}
	});
	
	//全部商品分类
	$('body').on('click','.filter-bg .all .btn',function(){
		$('.filter-bg .all .btn').removeClass('btn-black');
		$('.filter-bg .all .btn').addClass('btn-white');
		$(this).removeClass('btn-white');
		$(this).addClass('btn-black');
		var title = $(this).text();
		var data = $(this).attr('data-classifyId');
		$('.filter-bg').hide();
		var pro_load_page = 2,pro_load_flag = true;
		window.sessionStorage.setItem('searchkeyname','cat_id');
		window.sessionStorage.setItem('searchdata',data);
		var type = 'classify';
		searchResult.filterSearch('cat_id',data,type,title);
	});
	
	$('body').on('click','.product-list li',function(){
		var ids = $(this).attr('data-id');
		app.open('productDetail.html','productDetail',{id:ids},window.location.href,document.body.scrollTop);
	});
	
	$(window).scroll(function(){
		// 当滚动到距底部以上200像素
		var offset = $(".product-list").offset(),
		dataType = 0,
		data = {};
		var documentHeight = offset.top + $(".product-list").height();
		if (documentHeight - $(this).scrollTop() - $(this).height()<100){
				var kname = window.sessionStorage.getItem('searchkeyname');
				var data = 	window.sessionStorage.getItem('searchdata');
			pro_load_flag && searchResult.loadMore('/Goods/Index/keywords_search',kname,data);
		}
	});
}
